#define _CRT_SECURE_NO_WARNINGS 1

# include<bits/stdc++.h>
using namespace std;


class Solution {
    typedef pair<int, int> PII;
    int dx[4] = { 0,1,0,-1 };
    int dy[4] = { -1,0,1,0 };
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
        int now = image[sr][sc];
        int n = image.size(), m = image[0].size();
        if (now == color)   return image;
        queue<PII> q;
        q.push({ sr, sc });
        while (!q.empty())
        {
            auto [a, b] = q.front();
            q.pop();
            image[a][b] = color;
            for (int i = 0; i < 4; ++i)
            {
                int x = a + dx[i], y = b + dy[i];
                if (x >= 0 && x < n && y >= 0 && y < m && image[x][y] == now)
                {
                    q.push({ x, y });
                }
            }
        }
        return image;
    }
};